/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.goods.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.goods.entity.ZtSpecifications;
import com.jeesite.modules.goods.entity.ZtTrolley;
import com.jeesite.modules.goods.service.ZtGoodsService;
import com.jeesite.modules.goods.service.ZtSpecificationsService;
import com.jeesite.modules.goods.service.ZtTrolleyService;

/**
 * 购物车Controller
 * 
 * @author ThinkGem
 * @version 2018-04-22
 */
@Controller
@RequestMapping(value = "${adminPath}/ztTrolley")
public class ZtTrolleyController extends BaseController {

	@Autowired
	private ZtTrolleyService ztTrolleyService;
	
	@Autowired
	private ZtGoodsService ztGoodsService;
	
	@Autowired
	private ZtSpecificationsService ztSpecificationsService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ZtTrolley get(String id) {
		
		ZtTrolley ztTrolley= new ZtTrolley();
		if(StringUtils.isNotBlank(id)){
		ztTrolley.setId(id);
		ztTrolley=ztTrolleyService.get(ztTrolley);
		}
		return ztTrolley;
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("ztTrolley:ztTrolleyData:view")
	@RequestMapping(value = { "list", "" })
	public String list(ZtTrolley ztTrolley, Model model) {
		model.addAttribute("ztTrolley", ztTrolley);
	
		return "modules/ztTrolley/ztTrolleyList";
	}
	
	
	/**
	 * 查询列表
	 */
	
	@RequestMapping(value = "listself")
	public String listself(ZtTrolley ztTrolley, Model model) {
		model.addAttribute("ztTrolley", ztTrolley);
	
		return "modules/ztTrolley/ztTrolleyselfList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ztTrolley:ztTrolleyData:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ZtTrolley> listData(ZtTrolley ztTrolley, HttpServletRequest request,
			HttpServletResponse response,Model model) {
		
		Page<ZtTrolley> page = ztTrolleyService.findPage(new Page<ZtTrolley>(request,
				response), ztTrolley);
		
		for(ZtTrolley zt:page.getList()){
			String level=StringUtils.isBlank(zt.getZtAdmin().getLevel())?"0":zt.getZtAdmin().getLevel();
			if("0".equals(level)){
				zt.setPrice(zt.getNumCount()*zt.getZtGoods().getCommonPrice());
			}else{
				zt.setPrice(zt.getNumCount()*zt.getZtGoods().getDiscountPrice());
			}

			if(zt.getSpecifications()!=null && StringUtils.isNotBlank(zt.getSpecifications().getId())){
				ZtSpecifications ztSpecifications=ztSpecificationsService.get(zt.getSpecifications());
				zt.setGuige(ztSpecifications.getName());
			}else{
				zt.setGuige("-");
			}
			
		}
		
		return page;
	}
	
	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ztTrolley:ztTrolleyData:view")
	@RequestMapping(value = "form")
	public String form(ZtTrolley ztTrolley, Model model,HttpServletRequest request,HttpServletResponse response) {
		model.addAttribute("ztTrolley", ztTrolley);		
		return "modules/ztTrolley/ztTrolleyForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("ztTrolley:ztTrolleyData:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ZtTrolley ztTrolley) {
		
		ztTrolleyService.save(ztTrolley);
		return renderResult(Global.TRUE, text("保存购物车商品成功！"));
	}

}